package com.jeesite.modules.settlement.web.carpresettlement;

import com.jeesite.common.beetl.ext.fn.DictUtil;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.NumberUtils;
import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.common.mybatis.mapper.SqlMap;
import com.jeesite.common.mybatis.mapper.query.QueryAndor;
import com.jeesite.common.mybatis.mapper.query.QueryWhereEntity;
import com.jeesite.common.utils.excel.fieldtype.TcarType;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ar.dao.TarDao;
import com.jeesite.modules.arecieve.dao.TreceivableDetailDao;
import com.jeesite.modules.arecieve.entity.TreceivableDetail;
import com.jeesite.modules.base.entity.cars.Tcar;
import com.jeesite.modules.base.entity.customer.Customer;
import com.jeesite.modules.base.entity.investorfinancialinfo.InvestorFinancialInfo;
import com.jeesite.modules.base.entity.supplier.Supplier;
import com.jeesite.modules.base.service.customer.CustomerService;
import com.jeesite.modules.base.service.fixedexpense.FixedExpenseService;
import com.jeesite.modules.base.service.investorfinancialinfo.InvestorFinancialInfoService;
import com.jeesite.modules.base.service.supplier.SupplierService;
import com.jeesite.modules.custom.enumInfo.AccountType;
import com.jeesite.modules.financial.entity.billnegotiated.BillNegotiated;
import com.jeesite.modules.financial.entity.billnegotiated.BillNegotiatedDetail;
import com.jeesite.modules.financial.entity.taxclearance.TaxClearance;
import com.jeesite.modules.financial.entity.taxclearance.TaxClearanceDetail;
import com.jeesite.modules.financial.service.billnegotiated.BillNegotiatedService;
import com.jeesite.modules.financial.service.taxclearance.TaxClearanceService;
import com.jeesite.modules.order.dao.serviceorder.ServiceOrderDetailDao;
import com.jeesite.modules.order.entity.serviceorder.RelatedRecord;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.pay.dao.TpaymentDao;
import com.jeesite.modules.pay.dao.TpaymentDetailDao;
import com.jeesite.modules.pay.entity.Tpay;
import com.jeesite.modules.pay.entity.TpaymentDetail;
import com.jeesite.modules.prepay.dao.TprepaymentEstimatedDao;
import com.jeesite.modules.prepay.entity.Tprepayment;
import com.jeesite.modules.prepay.entity.TprepaymentEstimated;
import com.jeesite.modules.prepay.service.TprepaymentService;
import com.jeesite.modules.prerecieve.dao.TadvancesReceivedDao;
import com.jeesite.modules.prerecieve.dao.TadvancesReceivedEstimatedDao;
import com.jeesite.modules.prerecieve.entity.TadvancesReceived;
import com.jeesite.modules.prerecieve.entity.TadvancesReceivedEstimated;
import com.jeesite.modules.prerecieve.service.TadvancesReceivedService;
import com.jeesite.modules.settlement.dao.carpresettlement.TcarPresettlementDao;
import com.jeesite.modules.settlement.entity.carpresettlement.*;
import com.jeesite.modules.settlement.entity.customersettlement.CustomerSettlement;
import com.jeesite.modules.settlement.service.carpresettlement.TcarPresettlementService;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 车辆预结算报表Controller
 *
 * @author Kilo
 * @since 2021/3/23 10:18
 */
@Controller
@RequestMapping(value = "${adminPath}/settlement/carpresettlement/carPreSettlement")
public class CarPreSettlementController extends BaseController {

    @Autowired
    private BillNegotiatedService billNegotiatedService;

    @Autowired
    private TaxClearanceService taxClearanceService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private ServiceOrderService serviceOrderService;

    @Autowired
    private ServiceOrderDetailDao serviceOrderDetailDao;

    @Autowired
    private TadvancesReceivedService tadvancesReceivedService;

    @Autowired
    private InvestorFinancialInfoService investorFinancialInfoService;

    @Autowired
    private TprepaymentService tprepaymentService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private TadvancesReceivedDao tadvancesReceivedDao;

    @Autowired
    private TadvancesReceivedEstimatedDao tadvancesReceivedEstimatedDao;

    @Autowired
    private TprepaymentEstimatedDao tprepaymentEstimatedDao;

    @Autowired
    private TreceivableDetailDao treceivableDetailDao;

    @Autowired
    private TpaymentDetailDao tpaymentDetailDao;
@Autowired
private TcarPresettlementService tcarPresettlementService;
@Autowired
private FixedExpenseService fixedExpenseService;
@Autowired
private TcarPresettlementDao tcarPresettlementDao;

    /**
     * 获取数据
     */
    @ModelAttribute
    public TcarPresettlement get(String carPresettlementId, boolean isNewRecord) {
        return tcarPresettlementService.get(carPresettlementId, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("settlement:carpresettlement:carPreSettlement:view")
    @RequestMapping(value = {"searchlist", ""})
    public String searchlist(TcarPresettlement tcarPresettlement, Model model) {
        // 客户
        List<Customer> customerList = customerService.findList(new Customer());
        model.addAttribute("customerList",customerList);
        //服务订单
        List<ServiceOrder> serviceOrderList = serviceOrderService.findList(new ServiceOrder());
        model.addAttribute("serviceOrderList",serviceOrderList);
        // 车架号
        model.addAttribute("frameNumberList", serviceOrderDetailDao.findList(new ServiceOrderDetail()));

        model.addAttribute("tcarPresettlement", tcarPresettlement);
        return "modules/settlement/carpresettlement/carPresettlementList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("settlement:carpresettlement:carPreSettlement:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<TcarPresettlement> listData(TcarPresettlement tcarPresettlement, HttpServletRequest request, HttpServletResponse response) {
        tcarPresettlement.setPage(new Page<>(request, response));
        Page<TcarPresettlement> page = tcarPresettlementService.findPage(tcarPresettlement);
        return page;
    }
    /**
     * 删除数据
     */
    @RequiresPermissions("settlement:carpresettlement:carPreSettlement:delete")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(TcarPresettlement tcarPresettlement) {
        tcarPresettlementService.delete(tcarPresettlement);
        return renderResult(Global.TRUE, text("删除车辆预结算单成功！"));
    }


    /**
     * 查看表单详情
     */
    @RequiresPermissions("settlement:carpresettlement:carPreSettlement:view")
    @RequestMapping(value = "detail")
    public String detail(TcarPresettlement tcarPresettlement, Model model) {
        // 客户
        List<Customer> customerList = customerService.findList(new Customer());
        model.addAttribute("customerList",customerList);
        //服务订单
        List<ServiceOrder> serviceOrderList = serviceOrderService.findList(new ServiceOrder());
        model.addAttribute("serviceOrderList",serviceOrderList);
        // 车架号
        model.addAttribute("frameNumberList", serviceOrderDetailDao.findList(new ServiceOrderDetail()));

        model.addAttribute("tcarPresettlement", tcarPresettlement);
        // 相关记录
        List<RelatedRecord> relatedRecordList = ListUtils.newArrayList();
        // 预收单
        TadvancesReceived tadvancesReceived = new TadvancesReceived();
        tadvancesReceived.setServiceOrderId(tcarPresettlement.getServiceOrderId());
        tadvancesReceived.setParentBillCode(tcarPresettlement.getCarPresettlementId());
        List<TadvancesReceived> list4 = tadvancesReceivedService.findList(tadvancesReceived);
        for (TadvancesReceived tadvancesReceived1 : list4) {
            for (TadvancesReceivedEstimated tadvancesReceivedEstimated : tadvancesReceivedService.get(tadvancesReceived1).getTadvancesReceivedEstimatedList()) {
                RelatedRecord relatedRecord = new RelatedRecord();
                relatedRecord.setBillCode(tadvancesReceived1.getAdvancesReceivedCode());
                relatedRecord.setDate(tadvancesReceived1.getDate());
                relatedRecord.setExpenseItem(fixedExpenseService.get(tadvancesReceivedEstimated.getAdvancesReceivedType()).getFixedExpenseName());
                relatedRecord.setYushouAmount(tadvancesReceivedEstimated.getEstimatedReceivedAmount());
                relatedRecordList.add(relatedRecord);
            }
        }
        // 预付单
        Tprepayment tprepayment=new Tprepayment();
        tprepayment.setServiceOrderId(tcarPresettlement.getServiceOrderId());
        tprepayment.setParentBillCode(tcarPresettlement.getCarPresettlementId());
        List<Tprepayment> list5=tprepaymentService.findList(tprepayment);
        for (Tprepayment tprepayment1 : list5){
            for (TprepaymentEstimated tprepaymentEstimated :tprepaymentService.get(tprepayment1).getTprepaymentEstimatedList()){
                RelatedRecord relatedRecord = new RelatedRecord();
                relatedRecord.setBillCode(tprepayment1.getPrepaymentCode());
                relatedRecord.setDate(tprepayment1.getDate());
                relatedRecord.setExpenseItem(fixedExpenseService.get(tprepaymentEstimated.getFixedExpense()).getFixedExpenseName());
                relatedRecord.setYufuAmount(tprepaymentEstimated.getEstimatedPaymentAmount());
                relatedRecordList.add(relatedRecord);
            }
        }

        tcarPresettlement.setRelatedRecordList(relatedRecordList);
        return "modules/settlement/carpresettlement/carPresettlementDetail";
    }


    /**
     * 查询结算明细表单数据
     */
    @RequestMapping(value = "carPaymentReport")
    public String carPaymentReport(CustomerSettlement customerSettlement, Model model) {
        // 客户
        List<Customer> customerList = customerService.findList(new Customer());
        model.addAttribute("customerList",customerList);
        //国内资方
        Supplier gnzf = new Supplier();
        gnzf.setSupplierType("1");
        List<Supplier> gnzfList = supplierService.findList(gnzf);
        model.addAttribute("gnzfList", gnzfList);
        //服务订单
        List<ServiceOrder> serviceOrderList = serviceOrderService.findList(new ServiceOrder());
        model.addAttribute("serviceOrderList",serviceOrderList);
        model.addAttribute("customerSettlement", customerSettlement);
        return "modules/settlement/carpresettlement/carPaymentReport";
    }

    /**
     *动态获取serviceOrder
     */
    @RequestMapping(value = "getServiceOrderList2")
    @ResponseBody
    public List<ServiceOrder> getServiceOrderList2(ServiceOrder serviceOrder) {
        serviceOrder.setCustomer(customerService.get(serviceOrder.getCustomerId()));
        serviceOrder.setInvestor(supplierService.get(serviceOrder.getInvestorId()));
        List<ServiceOrder> list = serviceOrderService.findList(serviceOrder);
        return list;
    }

    /**
     *动态获取合计信息
     */
    @RequestMapping(value = "getTableData")
    @ResponseBody
    public List<Map<String,Object>> getTableData(ServiceOrder serviceOrder) {
        //合计信息
        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("serviceOrderId",serviceOrder.getServiceOrderId());
        List<Map<String,Object>> mainList = serviceOrderDetailDao.getCarJSReport(paramsMap);
        double a = 0;
        double b = 0;
        for(Map<String,Object> map:mainList){
            map.put("tabId","");
            if(ObjectUtils.isNotEmpty(map.get("rActualApAmount"))){
                a = Double.parseDouble(String.valueOf(map.get("rActualApAmount")));
            }
            if(ObjectUtils.isNotEmpty(map.get("pActualApAmount"))){
                b = Double.parseDouble(String.valueOf(map.get("pActualApAmount")));
            }
            map.put("gainAmount",NumberUtils.sub(a,b));
        }
        return mainList;
    }

    /**
     *动态获取款类信息
     */
    @RequestMapping(value = "getPayInfoTables")
    @ResponseBody
    public Map<String,Object> getPayInfoTables(String frameNumbers) {
        String[] frameNumberArr = frameNumbers.split(",");
        List<String> frameNumberList = Arrays.asList(frameNumberArr);
        Map<String,Object> map = new HashMap<>();

        //预收款信息列表
        List<TadvancesReceivedEstimated> tadvancesReceivedEstimatedList = tadvancesReceivedEstimatedDao.findAdvancesReceivedEstimatedInfo(frameNumberList);
        map.put("tadvancesReceivedEstimatedList",tadvancesReceivedEstimatedList);
        //获取预收客户余额总金额
        double advancedReceivedBalance = 0d;
        for (TadvancesReceivedEstimated tadvancesReceivedEstimated:tadvancesReceivedEstimatedList){
            advancedReceivedBalance = NumberUtils.add(advancedReceivedBalance, tadvancesReceivedEstimated.getReceivedAmountEstimated());
        }
        map.put("advancedReceivedBalance",advancedReceivedBalance);

        //预付款信息列表
        List<TprepaymentEstimated> tprepaymentEstimatedList = tprepaymentEstimatedDao.findPrepaymentEstimatedInfo(frameNumberList);
        map.put("tprepaymentEstimatedList",tprepaymentEstimatedList);
        //获取预付资方余额总金额
        double paymentAmountEstimatedBalance = 0d;
        for (TprepaymentEstimated tprepaymentEstimated:tprepaymentEstimatedList){
            paymentAmountEstimatedBalance = NumberUtils.add(paymentAmountEstimatedBalance,tprepaymentEstimated.getPaymentAmountEstimated());
        }
        map.put("paymentAmountEstimatedBalance",paymentAmountEstimatedBalance);

        //收款信息
        List<TreceivableDetail> treceivableDetailList = treceivableDetailDao.selectTreceivableDetailInfo(frameNumberList);
        map.put("treceivableDetailList",treceivableDetailList);

        //付款信息
        List<TpaymentDetail> tpayList = tpaymentDetailDao.selectPaymentDetailInfo(frameNumberList);
        map.put("tpayList",tpayList);
        return map;
    }

    @RequestMapping(value = {"list"})
    public String list(CarPreSettlement carPreSettlement, Model model){
        // 客户
        List<Customer> customerList = customerService.findList(new Customer());
        model.addAttribute("customerList",customerList);
        //服务订单
        List<ServiceOrder> serviceOrderList = serviceOrderService.findList(new ServiceOrder());
        model.addAttribute("serviceOrderList",serviceOrderList);
        // 车架号
        model.addAttribute("frameNumberList", serviceOrderDetailDao.findList(new ServiceOrderDetail()));
        carPreSettlement.setDate(new Date());
        model.addAttribute("carPreSettlement",carPreSettlement);
        return "modules/settlement/carpresettlement/carPreSettlement";
    }

    @RequiresPermissions("settlement:carpresettlement:carPreSettlement:view")
    @RequestMapping(value = "getList")
    @ResponseBody
    public CarPreSettlement getList(CarPreSettlement carPreSettlement){

        try {
            CarPreSettlement result = new CarPreSettlement();
            List<CarPreSettlementInvestor> carPreSettlementInvestorList = ListUtils.newArrayList();
            List<CarPreSettlementCustomer> carPreSettlementCustomerList = ListUtils.newArrayList();

            BillNegotiated billNegotiated = new BillNegotiated();
            billNegotiated.setFrameNumber(carPreSettlement.getFrameNumber());
            billNegotiated.setRowNumber(carPreSettlement.getRowNumber());
            billNegotiated.setStatus1("1");
            List<BillNegotiated> negotiatedList = billNegotiatedService.findList(billNegotiated);
            Double negotiatedAmount = 0D;// 押汇金额
            if(negotiatedList.size()>0) {//没有到单押汇的不取值，付款类型为TT的没有到单押汇单
                BillNegotiated negotiated = negotiatedList.get(0);
                negotiated=billNegotiatedService.get(negotiated);
                String n1Flag= negotiated.getN1Flag();//是否N+1
                negotiatedAmount = NumberUtils.mul(negotiated.getNegotiatedAmount(), carPreSettlement.getExchangeRate());

                TaxClearance taxClearance = new TaxClearance();
                taxClearance.setFrameNumber(carPreSettlement.getFrameNumber());
                taxClearance.setRowNumber(carPreSettlement.getRowNumber());
                taxClearance.setStatus1("1");
                List<TaxClearance> taxClearanceList = taxClearanceService.findList(taxClearance);
                Double prepaidAmount = 0D;// 代垫金额
                TaxClearance clearance = taxClearanceList.get(0);
                clearance=taxClearanceService.get(clearance);
                prepaidAmount = clearance.getPrepaidAmount();
                TadvancesReceived tadvancesReceived = new TadvancesReceived();
                tadvancesReceived.setServiceOrderId(carPreSettlement.getServiceOrderId());
                tadvancesReceived.setStatus1("4");
                Double cBenjinBalance = 0D;// 客户押汇本金
                Double cLixiBalance = 0D;// 客户押汇利息
                Double cYanqilixiBalance = 0D;// 客户押汇延期利息
                Double cdBenjinBalance = 0D;// 客户垫税本金
                Double cdLixiBalance = 0D;// 客户垫税利息
                Double cdYanqilixiBalance = 0D;// 客户垫税延期利息
                for (TadvancesReceived received : tadvancesReceivedService.findList(tadvancesReceived)) {
                    received=tadvancesReceivedService.get(received);
                    for (TadvancesReceivedEstimated tadvancesReceivedEstimated : received.getTadvancesReceivedEstimatedList()) {
                        if(tadvancesReceivedEstimated.getFrameNumber().equals(carPreSettlement.getFrameNumber())&&tadvancesReceivedEstimated.getRowNumber().equals(carPreSettlement.getRowNumber())){
                            if(AccountType.YHBJ.getCode().equals(tadvancesReceivedEstimated.getAdvancesReceivedType())){
                                cBenjinBalance = NumberUtils.add(cBenjinBalance, tadvancesReceivedEstimated.getReceivedSumActual());
                            }
                            if(AccountType.YHLX.getCode().equals(tadvancesReceivedEstimated.getAdvancesReceivedType())){
                                cLixiBalance = NumberUtils.add(cLixiBalance, tadvancesReceivedEstimated.getReceivedSumActual());
                            }
                            if(AccountType.YHYQLX.getCode().equals(tadvancesReceivedEstimated.getAdvancesReceivedType())){
                                cYanqilixiBalance = NumberUtils.add(cYanqilixiBalance, tadvancesReceivedEstimated.getReceivedSumActual());
                            }
                            if(AccountType.DSBJ.getCode().equals(tadvancesReceivedEstimated.getAdvancesReceivedType())){
                                cdBenjinBalance = NumberUtils.add(cdBenjinBalance, tadvancesReceivedEstimated.getReceivedSumActual());
                            }
                            if(AccountType.DSLX.getCode().equals(tadvancesReceivedEstimated.getAdvancesReceivedType())){
                                cdLixiBalance = NumberUtils.add(cdLixiBalance, tadvancesReceivedEstimated.getReceivedSumActual());
                            }
                            if(AccountType.DSYQLX.getCode().equals(tadvancesReceivedEstimated.getAdvancesReceivedType())){
                                cdYanqilixiBalance = NumberUtils.add(cdYanqilixiBalance, tadvancesReceivedEstimated.getReceivedSumActual());
                            }
                        }
                    }
                }
                Double iBenjinBalance = 0D;// 资方押汇本金
                Double iLixiBalance = 0D;// 资方押汇利息
                Double iYanqilixiBalance = 0D;// 资方押汇延期利息
                Double idBenjinBalance = 0D;// 资方垫税本金
                Double idLixiBalance = 0D;// 资方垫税利息
                Double idYanqilixiBalance = 0D;// 资方垫税延期利息
                Tprepayment tprepayment = new Tprepayment();
                tprepayment.setServiceOrderId(carPreSettlement.getServiceOrderId());
                tprepayment.setStatus1("4");
                for (Tprepayment tprepayment1 : tprepaymentService.findList(tprepayment)) {
                    tprepayment1=tprepaymentService.get(tprepayment1);
                    for (TprepaymentEstimated tprepaymentEstimated : tprepayment1.getTprepaymentEstimatedList()) {
                        if(tprepaymentEstimated.getFrameNumber().equals(carPreSettlement.getFrameNumber())&&tprepaymentEstimated.getRowNumber().equals(carPreSettlement.getRowNumber())){
                            if(AccountType.YHBJ.getCode().equals(tprepaymentEstimated.getPrepaymentType())){
                                iBenjinBalance = NumberUtils.add(cBenjinBalance, tprepaymentEstimated.getPaymentSumActual());
                            }
                            if(AccountType.YHLX.getCode().equals(tprepaymentEstimated.getPrepaymentType())){
                                iLixiBalance = NumberUtils.add(cLixiBalance, tprepaymentEstimated.getPaymentSumActual());
                            }
                            if(AccountType.YHYQLX.getCode().equals(tprepaymentEstimated.getPrepaymentType())){
                                iYanqilixiBalance = NumberUtils.add(cYanqilixiBalance, tprepaymentEstimated.getPaymentSumActual());
                            }
                            if(AccountType.DSBJ.getCode().equals(tprepaymentEstimated.getPrepaymentType())){
                                idBenjinBalance = NumberUtils.add(cdBenjinBalance, tprepaymentEstimated.getPaymentSumActual());
                            }
                            if(AccountType.DSLX.getCode().equals(tprepaymentEstimated.getPrepaymentType())){
                                idLixiBalance = NumberUtils.add(cdLixiBalance, tprepaymentEstimated.getPaymentSumActual());
                            }
                            if(AccountType.DSYQLX.getCode().equals(tprepaymentEstimated.getPrepaymentType())){
                                idYanqilixiBalance = NumberUtils.add(cdYanqilixiBalance, tprepaymentEstimated.getPaymentSumActual());
                            }
                        }
                    }
                }
                // 资方到单押汇-本金
                CarPreSettlementInvestor carPreSettlementInvestor = new CarPreSettlementInvestor();
                carPreSettlementInvestor.setCarId(negotiated.getCarId());
                carPreSettlementInvestor.setCarName(negotiated.getTcar().getCarName());
                carPreSettlementInvestor.setFrameNumber(negotiated.getFrameNumber());
                carPreSettlementInvestor.setAmount(negotiated.getUnitPrice());
                carPreSettlementInvestor.setCurrency(negotiated.getCurrency());
                carPreSettlementInvestor.setPreSettlementAmount(negotiatedAmount);
                carPreSettlementInvestor.setPreSettlementBalance(iBenjinBalance);
                carPreSettlementInvestor.setDifference(NumberUtils.sub(negotiatedAmount, iBenjinBalance));
                carPreSettlementInvestor.setType(AccountType.YHBJ.getInfo());
                carPreSettlementInvestor.setCurrencyId(negotiated.getCurrency());
                carPreSettlementInvestor.setTypeId(AccountType.YHBJ.getCode());
                carPreSettlementInvestorList.add(carPreSettlementInvestor);

                // 资方到单押汇-利息
                CarPreSettlementInvestor carPreSettlementInvestor1 = new CarPreSettlementInvestor();
                carPreSettlementInvestor1.setCarId(negotiated.getCarId());
                carPreSettlementInvestor1.setCarName(negotiated.getTcar().getCarName());
                carPreSettlementInvestor1.setFrameNumber(negotiated.getFrameNumber());
                carPreSettlementInvestor1.setAmount(negotiated.getUnitPrice());
                carPreSettlementInvestor1.setCurrency(negotiated.getCurrency());
                Double iddyhLixi = 0D; // 利息
                Double investorDays = 0D; // 资方天数差
                Double investorInterestRatio = NumberUtils.div(Double.parseDouble(negotiated.getInvestorInterestRatio()), 100); //资方正常利率
                Long investorYearDays = negotiated.getInvestorYearDays(); // 资方年天数


                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                Date now = df.parse(df.format(new Date()));
                if(now.compareTo(negotiated.getInvestorEndDate()) >= 0){
                    investorDays = DateUtils.getDistanceOfTwoDate(negotiated.getStartDate(), negotiated.getInvestorEndDate());
                } else {
                    investorDays = DateUtils.getDistanceOfTwoDate(negotiated.getStartDate(), now);
                }
                if("1".equals(n1Flag)) {
                    investorDays=investorDays+1;
                }
                if(investorDays != 0){

                    iddyhLixi = NumberUtils.mul(NumberUtils.div(NumberUtils.mul(negotiatedAmount, investorInterestRatio),investorYearDays), investorDays);
                }
                carPreSettlementInvestor1.setPreSettlementAmount(iddyhLixi);
                carPreSettlementInvestor1.setPreSettlementBalance(iLixiBalance);
                carPreSettlementInvestor1.setDifference(NumberUtils.sub(iddyhLixi, iLixiBalance));
                carPreSettlementInvestor1.setType(AccountType.YHLX.getInfo());
                carPreSettlementInvestor1.setTypeId(AccountType.YHLX.getCode());
                carPreSettlementInvestor1.setCurrencyId(negotiated.getCurrency());
                carPreSettlementInvestorList.add(carPreSettlementInvestor1);

                // 资方到单押汇-延期利息
                CarPreSettlementInvestor carPreSettlementInvestor2 = new CarPreSettlementInvestor();
                carPreSettlementInvestor2.setCarId(negotiated.getCarId());
                carPreSettlementInvestor2.setCarName(negotiated.getTcar().getCarName());
                carPreSettlementInvestor2.setFrameNumber(negotiated.getFrameNumber());
                carPreSettlementInvestor2.setAmount(negotiated.getUnitPrice());
                carPreSettlementInvestor2.setCurrency(negotiated.getCurrency());
                Double investorInterestTotal = 0D;//资方延期利息合计
                for (BillNegotiatedDetail billNegotiatedDetail : negotiated.getBillNegotiatedDetailList()) {
                    if ("1".equals(billNegotiatedDetail.getType())) {
                        Double investorDetailRatio = NumberUtils.div(Double.parseDouble(billNegotiatedDetail.getNegotiatedRate()), 100); //资方利率
                        Double investorDetailDays = 0D;
                        if(now.compareTo(billNegotiatedDetail.getStartDate()) >= 0 && now.compareTo(billNegotiatedDetail.getEndDate()) <= 0){
                            investorDetailDays = DateUtils.getDistanceOfTwoDate(billNegotiatedDetail.getStartDate(), now);
                        } else if(now.compareTo(billNegotiatedDetail.getStartDate()) >= 0 && now.compareTo(billNegotiatedDetail.getEndDate()) > 0){
                            investorDetailDays = DateUtils.getDistanceOfTwoDate(billNegotiatedDetail.getStartDate(), billNegotiatedDetail.getEndDate());
                        }
                        Double interest = NumberUtils.mul(NumberUtils.div(NumberUtils.mul(negotiatedAmount, investorDetailRatio),investorYearDays), investorDetailDays);
                        investorInterestTotal = NumberUtils.add(investorInterestTotal, interest);
                    }
                }
                carPreSettlementInvestor2.setPreSettlementAmount(investorInterestTotal);
                carPreSettlementInvestor2.setPreSettlementBalance(iYanqilixiBalance);
                carPreSettlementInvestor2.setDifference(NumberUtils.sub(investorInterestTotal, iYanqilixiBalance));
                carPreSettlementInvestor2.setType(AccountType.YHYQLX.getInfo());
                carPreSettlementInvestor2.setTypeId(AccountType.YHYQLX.getCode());
                carPreSettlementInvestor2.setCurrencyId(negotiated.getCurrency());
                carPreSettlementInvestorList.add(carPreSettlementInvestor2);

                // 资方通关缴税-本金
                CarPreSettlementInvestor carPreSettlementInvestor5 = new CarPreSettlementInvestor();
                carPreSettlementInvestor5.setCarId(clearance.getCarId());
                carPreSettlementInvestor5.setCarName(clearance.getTcar().getCarName());
                carPreSettlementInvestor5.setFrameNumber(clearance.getFrameNumber());
                carPreSettlementInvestor5.setAmount(clearance.getForeignCurrencyPrice());
                carPreSettlementInvestor5.setCurrency(clearance.getCurrency());
                carPreSettlementInvestor5.setPreSettlementAmount(prepaidAmount);
                carPreSettlementInvestor5.setPreSettlementBalance(idBenjinBalance);
                carPreSettlementInvestor5.setDifference(NumberUtils.sub(prepaidAmount, idBenjinBalance));
                carPreSettlementInvestor5.setType(AccountType.DSBJ.getInfo());
                carPreSettlementInvestor5.setTypeId(AccountType.DSBJ.getCode());
                carPreSettlementInvestor5.setCurrencyId(negotiated.getCurrency());
                carPreSettlementInvestorList.add(carPreSettlementInvestor5);

                // 资方通关缴税-利息
                CarPreSettlementInvestor carPreSettlementInvestor3 = new CarPreSettlementInvestor();
                carPreSettlementInvestor3.setCarId(clearance.getCarId());
                carPreSettlementInvestor3.setCarName(clearance.getTcar().getCarName());
                carPreSettlementInvestor3.setFrameNumber(clearance.getFrameNumber());
                carPreSettlementInvestor3.setAmount(clearance.getForeignCurrencyPrice());
                carPreSettlementInvestor3.setCurrency(clearance.getCurrency());
                InvestorFinancialInfo investorFinancialInfo = new InvestorFinancialInfo();
                investorFinancialInfo.setSupplier(clearance.getInvestor());
                investorFinancialInfo.setFinancingType("1");
                InvestorFinancialInfo info = investorFinancialInfoService.findList(investorFinancialInfo).get(0);
                Long investorYearDay = info.getDays();// 资方年天数
                Double interest = 0D;
                for (TaxClearanceDetail taxClearanceDetail : clearance.getTaxClearanceDetailList()) {
                    if("1".equals(taxClearanceDetail.getType())){
                        // 计算资方利息
                        Double investorDetailRatio = NumberUtils.div(Double.parseDouble(taxClearanceDetail.getInterestRate()), 100); //资方利率
                        Double investorDetailDays = 0D;
                        if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) <= 0){
                            investorDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), now);
                        } else if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) > 0){
                            investorDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), taxClearanceDetail.getEndDate());
                        }
                        interest = NumberUtils.mul(NumberUtils.div(NumberUtils.mul(prepaidAmount, investorDetailRatio),investorYearDay), investorDetailDays);
                        break;
                    }
                }
                carPreSettlementInvestor3.setPreSettlementAmount(interest);
                carPreSettlementInvestor3.setPreSettlementBalance(idLixiBalance);
                carPreSettlementInvestor3.setDifference(NumberUtils.sub(interest, idLixiBalance));
                carPreSettlementInvestor3.setType(AccountType.DSLX.getInfo());
                carPreSettlementInvestor3.setTypeId(AccountType.DSLX.getCode());
                carPreSettlementInvestor3.setCurrencyId(negotiated.getCurrency());
                carPreSettlementInvestorList.add(carPreSettlementInvestor3);

                // 资方通关缴税-延期利息
                CarPreSettlementInvestor carPreSettlementInvestor4 = new CarPreSettlementInvestor();
                carPreSettlementInvestor4.setCarId(clearance.getCarId());
                carPreSettlementInvestor4.setCarName(clearance.getTcar().getCarName());
                carPreSettlementInvestor4.setFrameNumber(clearance.getFrameNumber());
                carPreSettlementInvestor4.setAmount(clearance.getForeignCurrencyPrice());
                carPreSettlementInvestor4.setCurrency(clearance.getCurrency());
                Double investorInterestTotal1 = 0D;//资方延期利息合计
                for (TaxClearanceDetail taxClearanceDetail : clearance.getTaxClearanceDetailList()) {
                    if("1".equals(taxClearanceDetail.getType())){
                        // 计算资方利息
                        Double investorDetailRatio = NumberUtils.div(Double.parseDouble(taxClearanceDetail.getInterestRate()), 100); //资方利率
                        Double investorDetailDays = 0D;
                        if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) <= 0){
                            investorDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), now);
                        } else if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) > 0){
                            investorDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), taxClearanceDetail.getEndDate());
                        }
                        Double interest1 = NumberUtils.mul(NumberUtils.div(NumberUtils.mul(prepaidAmount, investorDetailRatio),investorYearDay), investorDetailDays);
                        investorInterestTotal1 = NumberUtils.add(investorInterestTotal1, interest1);
                    }
                }
                carPreSettlementInvestor4.setPreSettlementAmount(NumberUtils.sub(investorInterestTotal1, interest));// 减去第一行的利息
                carPreSettlementInvestor4.setPreSettlementBalance(idYanqilixiBalance);
                carPreSettlementInvestor4.setDifference(NumberUtils.sub(NumberUtils.sub(investorInterestTotal1, interest),idYanqilixiBalance));
                carPreSettlementInvestor4.setType(AccountType.DSYQLX.getInfo());
                carPreSettlementInvestor4.setTypeId(AccountType.DSYQLX.getCode());
                carPreSettlementInvestor4.setCurrencyId(negotiated.getCurrency());
                carPreSettlementInvestorList.add(carPreSettlementInvestor4);

                result.setCarPreSettlementInvestorList(carPreSettlementInvestorList);

                // 客户到单押汇-本金
                CarPreSettlementCustomer carPreSettlementCustomer = new CarPreSettlementCustomer();
                carPreSettlementCustomer.setCarId(negotiated.getCarId());
                carPreSettlementCustomer.setCarName(negotiated.getTcar().getCarName());
                carPreSettlementCustomer.setFrameNumber(negotiated.getFrameNumber());
                carPreSettlementCustomer.setAmount(negotiated.getUnitPrice());
                carPreSettlementCustomer.setCurrency(negotiated.getCurrency());
                carPreSettlementCustomer.setPreSettlementAmount(negotiatedAmount);
                carPreSettlementCustomer.setPreSettlementBalance(cBenjinBalance);
                carPreSettlementCustomer.setDifference(NumberUtils.sub(negotiatedAmount,cBenjinBalance));
                carPreSettlementCustomer.setType(AccountType.YHBJ.getInfo());
                carPreSettlementCustomer.setTypeId(AccountType.YHBJ.getCode());
                carPreSettlementCustomer.setCurrencyId(negotiated.getCurrency());
                carPreSettlementCustomerList.add(carPreSettlementCustomer);

                // 客户到单押汇-利息
                CarPreSettlementCustomer carPreSettlementCustomer1 = new CarPreSettlementCustomer();
                carPreSettlementCustomer1.setCarId(negotiated.getCarId());
                carPreSettlementCustomer1.setCarName(negotiated.getTcar().getCarName());
                carPreSettlementCustomer1.setFrameNumber(negotiated.getFrameNumber());
                carPreSettlementCustomer1.setAmount(negotiated.getUnitPrice());
                carPreSettlementCustomer1.setCurrency(negotiated.getCurrency());
                Double cddyhLixi = 0D; // 利息
                Double customerDays = 0D; // 客户天数差
                Double customerInterestRatio = NumberUtils.div(Double.parseDouble(negotiated.getCustomerInterestRatio()), 100); //客户正常利率
                if(now.compareTo(negotiated.getCustomerEndDate()) >= 0){
                    customerDays = DateUtils.getDistanceOfTwoDate(negotiated.getStartDate(), negotiated.getCustomerEndDate());
                } else {
                    customerDays = DateUtils.getDistanceOfTwoDate(negotiated.getStartDate(), now);
                }
                if("1".equals(n1Flag)) {
                    customerDays=customerDays+1;
                }
                if(customerDays != 0){
                    cddyhLixi = NumberUtils.mul(NumberUtils.div(NumberUtils.mul(negotiatedAmount, customerInterestRatio),360), customerDays);
                }

                carPreSettlementCustomer1.setPreSettlementAmount(cddyhLixi);
                carPreSettlementCustomer1.setPreSettlementBalance(cLixiBalance);
                carPreSettlementCustomer1.setDifference(NumberUtils.sub(cddyhLixi, cLixiBalance));
                carPreSettlementCustomer1.setType(AccountType.YHLX.getInfo());
                carPreSettlementCustomer1.setTypeId(AccountType.YHLX.getCode());
                carPreSettlementCustomer1.setCurrencyId(negotiated.getCurrency());
                carPreSettlementCustomerList.add(carPreSettlementCustomer1);

                // 客户到单押汇-延期利息
                CarPreSettlementCustomer carPreSettlementCustomer2 = new CarPreSettlementCustomer();
                carPreSettlementCustomer2.setCarId(negotiated.getCarId());
                carPreSettlementCustomer2.setCarName(negotiated.getTcar().getCarName());
                carPreSettlementCustomer2.setFrameNumber(negotiated.getFrameNumber());
                carPreSettlementCustomer2.setAmount(negotiated.getUnitPrice());
                carPreSettlementCustomer2.setCurrency(negotiated.getCurrency());
                Double customerInterestTotal = 0D;//客户延期利息合计
                for (BillNegotiatedDetail billNegotiatedDetail : negotiated.getBillNegotiatedDetailList()) {
                    if("2".equals(billNegotiatedDetail.getType())){
                        Double customerDetailRatio = NumberUtils.div(Double.parseDouble(billNegotiatedDetail.getNegotiatedRate()), 100); //客户利率
                        Double customerDetailDays = 0D;
                        if(now.compareTo(billNegotiatedDetail.getStartDate()) >= 0 && now.compareTo(billNegotiatedDetail.getEndDate()) <= 0){
                            customerDetailDays = DateUtils.getDistanceOfTwoDate(billNegotiatedDetail.getStartDate(), now);
                        } else if(now.compareTo(billNegotiatedDetail.getStartDate()) >= 0 && now.compareTo(billNegotiatedDetail.getEndDate()) > 0){
                            customerDetailDays = DateUtils.getDistanceOfTwoDate(billNegotiatedDetail.getStartDate(), billNegotiatedDetail.getEndDate());
                        }
                        Double interest1 = NumberUtils.mul(NumberUtils.div(NumberUtils.mul(negotiatedAmount, customerDetailRatio),360), customerDetailDays);
                        customerInterestTotal = NumberUtils.add(customerInterestTotal, interest1);
                    }
                }
                carPreSettlementCustomer2.setPreSettlementAmount(customerInterestTotal);
                carPreSettlementCustomer2.setPreSettlementBalance(cYanqilixiBalance);
                carPreSettlementCustomer2.setDifference(NumberUtils.sub(customerInterestTotal, cYanqilixiBalance));
                carPreSettlementCustomer2.setType(AccountType.YHYQLX.getInfo());
                carPreSettlementCustomer2.setTypeId(AccountType.YHYQLX.getCode());
                carPreSettlementCustomer2.setCurrencyId(negotiated.getCurrency());
                carPreSettlementCustomerList.add(carPreSettlementCustomer2);

                // 客户通关缴税-本金
                CarPreSettlementCustomer carPreSettlementCustomer3 = new CarPreSettlementCustomer();
                carPreSettlementCustomer3.setCarId(clearance.getCarId());
                carPreSettlementCustomer3.setCarName(clearance.getTcar().getCarName());
                carPreSettlementCustomer3.setFrameNumber(clearance.getFrameNumber());
                carPreSettlementCustomer3.setAmount(clearance.getForeignCurrencyPrice());
                carPreSettlementCustomer3.setCurrency(clearance.getCurrency());
                carPreSettlementCustomer3.setPreSettlementAmount(prepaidAmount);
                carPreSettlementCustomer3.setPreSettlementBalance(cdBenjinBalance);
                carPreSettlementCustomer3.setDifference(NumberUtils.sub(prepaidAmount, cdBenjinBalance));
                carPreSettlementCustomer3.setType(AccountType.DSBJ.getInfo());
                carPreSettlementCustomer3.setTypeId(AccountType.DSBJ.getCode());
                carPreSettlementCustomer3.setCurrencyId(negotiated.getCurrency());
                carPreSettlementCustomerList.add(carPreSettlementCustomer3);

                // 客户通关缴税-利息
                CarPreSettlementCustomer carPreSettlementCustomer4 = new CarPreSettlementCustomer();
                carPreSettlementCustomer4.setCarId(clearance.getCarId());
                carPreSettlementCustomer4.setCarName(clearance.getTcar().getCarName());
                carPreSettlementCustomer4.setFrameNumber(clearance.getFrameNumber());
                carPreSettlementCustomer4.setAmount(clearance.getForeignCurrencyPrice());
                carPreSettlementCustomer4.setCurrency(clearance.getCurrency());
                Double interest2 = 0D;
                for (TaxClearanceDetail taxClearanceDetail : clearance.getTaxClearanceDetailList()) {
                    if("2".equals(taxClearanceDetail.getType())){
                        Double customerDetailRatio = NumberUtils.div(Double.parseDouble(taxClearanceDetail.getInterestRate()), 100); //客户利率
                        Double customerDetailDays = 0D;
                        if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) <= 0){
                            customerDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), now);
                        } else if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) > 0){
                            customerDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), taxClearanceDetail.getEndDate());
                        }
                        interest2 = NumberUtils.mul(NumberUtils.div(NumberUtils.mul(prepaidAmount, customerDetailRatio),360), customerDetailDays);
                        break;
                    }
                }
                carPreSettlementCustomer4.setPreSettlementAmount(interest2);
                carPreSettlementCustomer4.setPreSettlementBalance(cdLixiBalance);
                carPreSettlementCustomer4.setDifference(NumberUtils.sub(interest2, cdLixiBalance));
                carPreSettlementCustomer4.setType(AccountType.DSLX.getInfo());
                carPreSettlementCustomer4.setTypeId(AccountType.DSLX.getCode());
                carPreSettlementCustomer4.setCurrencyId(negotiated.getCurrency());
                carPreSettlementCustomerList.add(carPreSettlementCustomer4);

                // 客户通关缴税-延期利息
                CarPreSettlementCustomer carPreSettlementCustomer5 = new CarPreSettlementCustomer();
                carPreSettlementCustomer5.setCarId(clearance.getCarId());
                carPreSettlementCustomer5.setCarName(clearance.getTcar().getCarName());
                carPreSettlementCustomer5.setFrameNumber(clearance.getFrameNumber());
                carPreSettlementCustomer5.setAmount(clearance.getForeignCurrencyPrice());
                carPreSettlementCustomer5.setCurrency(clearance.getCurrency());
                Double customerInterestTotal1 = 0D;//客户延期利息合计
                for (TaxClearanceDetail taxClearanceDetail : clearance.getTaxClearanceDetailList()) {
                    if ("2".equals(taxClearanceDetail.getType())) {
                        Double customerDetailRatio = NumberUtils.div(Double.parseDouble(taxClearanceDetail.getInterestRate()), 100); //客户利率
                        Double customerDetailDays = 0D;
                        if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) <= 0){
                            customerDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), now);
                        } else if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) > 0){
                            customerDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), taxClearanceDetail.getEndDate());
                        }
                        Double interest3 = NumberUtils.mul(NumberUtils.div(NumberUtils.mul(prepaidAmount, customerDetailRatio),360), customerDetailDays);
                        customerInterestTotal1 = NumberUtils.add(customerInterestTotal1, interest3);
                    }
                }
                carPreSettlementCustomer5.setPreSettlementAmount(NumberUtils.sub(customerInterestTotal1, interest2));// 减去第一行利息
                carPreSettlementCustomer5.setPreSettlementBalance(cdYanqilixiBalance);
                carPreSettlementCustomer5.setDifference(NumberUtils.sub(NumberUtils.sub(customerInterestTotal1, interest2), cdYanqilixiBalance));
                carPreSettlementCustomer5.setType(AccountType.DSYQLX.getInfo());
                carPreSettlementCustomer5.setTypeId(AccountType.DSYQLX.getCode());
                carPreSettlementCustomer5.setCurrencyId(negotiated.getCurrency());
                carPreSettlementCustomerList.add(carPreSettlementCustomer5);

                result.setCarPreSettlementCustomerList(carPreSettlementCustomerList);
            }else{//付款方式为TT的，没有到单押汇相关
                TaxClearance taxClearance = new TaxClearance();
                taxClearance.setFrameNumber(carPreSettlement.getFrameNumber());
                taxClearance.setRowNumber(carPreSettlement.getRowNumber());
                taxClearance.setStatus1("1");
                List<TaxClearance> taxClearanceList = taxClearanceService.findList(taxClearance);
                Double prepaidAmount = 0D;// 代垫金额
                if(taxClearanceList.size()>0){
                TaxClearance clearance = taxClearanceList.get(0);
                    clearance=taxClearanceService.get(clearance);
                prepaidAmount = clearance.getPrepaidAmount();
                TadvancesReceived tadvancesReceived = new TadvancesReceived();
                tadvancesReceived.setServiceOrderId(carPreSettlement.getServiceOrderId());
                tadvancesReceived.setStatus1("4");

                Double cdBenjinBalance = 0D;// 客户垫税本金
                Double cdLixiBalance = 0D;// 客户垫税利息
                Double cdYanqilixiBalance = 0D;// 客户垫税延期利息
                for (TadvancesReceived received : tadvancesReceivedService.findList(tadvancesReceived)) {
                    received=tadvancesReceivedService.get(received);
                    for (TadvancesReceivedEstimated tadvancesReceivedEstimated : received.getTadvancesReceivedEstimatedList()) {
                        if(tadvancesReceivedEstimated.getFrameNumber().equals(carPreSettlement.getFrameNumber())&&tadvancesReceivedEstimated.getRowNumber().equals(carPreSettlement.getRowNumber())){

                            if(AccountType.DSBJ.getCode().equals(tadvancesReceivedEstimated.getAdvancesReceivedType())){
                                cdBenjinBalance = NumberUtils.add(cdBenjinBalance, tadvancesReceivedEstimated.getReceivedSumActual());
                            }
                            if(AccountType.DSLX.getCode().equals(tadvancesReceivedEstimated.getAdvancesReceivedType())){
                                cdLixiBalance = NumberUtils.add(cdLixiBalance, tadvancesReceivedEstimated.getReceivedSumActual());
                            }
                            if(AccountType.DSYQLX.getCode().equals(tadvancesReceivedEstimated.getAdvancesReceivedType())){
                                cdYanqilixiBalance = NumberUtils.add(cdYanqilixiBalance, tadvancesReceivedEstimated.getReceivedSumActual());
                            }
                        }
                    }
                }

                Double idBenjinBalance = 0D;// 资方垫税本金
                Double idLixiBalance = 0D;// 资方垫税利息
                Double idYanqilixiBalance = 0D;// 资方垫税延期利息
                Tprepayment tprepayment = new Tprepayment();
                tprepayment.setServiceOrderId(carPreSettlement.getServiceOrderId());
                tprepayment.setStatus1("4");
                for (Tprepayment tprepayment1 : tprepaymentService.findList(tprepayment)) {
                    tprepayment1=tprepaymentService.get(tprepayment1);
                    for (TprepaymentEstimated tprepaymentEstimated : tprepayment1.getTprepaymentEstimatedList()) {
                        if(tprepaymentEstimated.getFrameNumber().equals(carPreSettlement.getFrameNumber())&&tprepaymentEstimated.getRowNumber().equals(carPreSettlement.getRowNumber())){

                            if(AccountType.DSBJ.getCode().equals(tprepaymentEstimated.getPrepaymentType())){
                                idBenjinBalance = NumberUtils.add(cdBenjinBalance, tprepaymentEstimated.getPaymentSumActual());
                            }
                            if(AccountType.DSLX.getCode().equals(tprepaymentEstimated.getPrepaymentType())){
                                idLixiBalance = NumberUtils.add(cdLixiBalance, tprepaymentEstimated.getPaymentSumActual());
                            }
                            if(AccountType.DSYQLX.getCode().equals(tprepaymentEstimated.getPrepaymentType())){
                                idYanqilixiBalance = NumberUtils.add(cdYanqilixiBalance, tprepaymentEstimated.getPaymentSumActual());
                            }
                        }
                    }
                }


                Double iddyhLixi = 0D; // 利息
                Double investorDays = 0D; // 资方天数差

                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                Date now = df.parse(df.format(new Date()));




                // 资方通关缴税-本金
                CarPreSettlementInvestor carPreSettlementInvestor5 = new CarPreSettlementInvestor();
                carPreSettlementInvestor5.setCarId(clearance.getCarId());
                carPreSettlementInvestor5.setCarName(clearance.getTcar().getCarName());
                carPreSettlementInvestor5.setFrameNumber(clearance.getFrameNumber());
                carPreSettlementInvestor5.setAmount(clearance.getForeignCurrencyPrice());
                carPreSettlementInvestor5.setCurrency(clearance.getCurrency());
                carPreSettlementInvestor5.setPreSettlementAmount(prepaidAmount);
                carPreSettlementInvestor5.setPreSettlementBalance(idBenjinBalance);
                carPreSettlementInvestor5.setDifference(NumberUtils.sub(prepaidAmount, idBenjinBalance));
                carPreSettlementInvestor5.setType(AccountType.DSBJ.getInfo());
                carPreSettlementInvestor5.setTypeId(AccountType.DSBJ.getCode());
                carPreSettlementInvestor5.setCurrencyId(taxClearance.getCurrency());
                carPreSettlementInvestorList.add(carPreSettlementInvestor5);

                // 资方通关缴税-利息
                CarPreSettlementInvestor carPreSettlementInvestor3 = new CarPreSettlementInvestor();
                carPreSettlementInvestor3.setCarId(clearance.getCarId());
                carPreSettlementInvestor3.setCarName(clearance.getTcar().getCarName());
                carPreSettlementInvestor3.setFrameNumber(clearance.getFrameNumber());
                carPreSettlementInvestor3.setAmount(clearance.getForeignCurrencyPrice());
                carPreSettlementInvestor3.setCurrency(clearance.getCurrency());
                InvestorFinancialInfo investorFinancialInfo = new InvestorFinancialInfo();
                investorFinancialInfo.setSupplier(taxClearance.getInvestor());
                investorFinancialInfo.setFinancingType("1");
                InvestorFinancialInfo info = investorFinancialInfoService.findList(investorFinancialInfo).get(0);
                Long investorYearDay = info.getDays();// 资方年天数
                Double interest = 0D;
                for (TaxClearanceDetail taxClearanceDetail : taxClearance.getTaxClearanceDetailList()) {
                    if("1".equals(taxClearanceDetail.getType())){
                        // 计算资方利息
                        Double investorDetailRatio = NumberUtils.div(Double.parseDouble(taxClearanceDetail.getInterestRate()), 100); //资方利率
                        Double investorDetailDays = 0D;
                        if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) <= 0){
                            investorDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), now);
                        } else if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) > 0){
                            investorDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), taxClearanceDetail.getEndDate());
                        }
                        interest = NumberUtils.mul(NumberUtils.div(NumberUtils.mul(prepaidAmount, investorDetailRatio),investorYearDay), investorDetailDays);
                        break;
                    }
                }
                carPreSettlementInvestor3.setPreSettlementAmount(interest);
                carPreSettlementInvestor3.setPreSettlementBalance(idLixiBalance);
                carPreSettlementInvestor3.setDifference(NumberUtils.sub(interest, idLixiBalance));
                carPreSettlementInvestor3.setType(AccountType.DSLX.getInfo());
                carPreSettlementInvestor3.setTypeId(AccountType.DSLX.getCode());
                carPreSettlementInvestor3.setCurrencyId(taxClearance.getCurrency());
                carPreSettlementInvestorList.add(carPreSettlementInvestor3);

                // 资方通关缴税-延期利息
                CarPreSettlementInvestor carPreSettlementInvestor4 = new CarPreSettlementInvestor();
                carPreSettlementInvestor4.setCarId(clearance.getCarId());
                carPreSettlementInvestor4.setCarName(clearance.getTcar().getCarName());
                carPreSettlementInvestor4.setFrameNumber(clearance.getFrameNumber());
                carPreSettlementInvestor4.setAmount(clearance.getForeignCurrencyPrice());
                carPreSettlementInvestor4.setCurrency(clearance.getCurrency());
                Double investorInterestTotal1 = 0D;//资方延期利息合计
                for (TaxClearanceDetail taxClearanceDetail : taxClearance.getTaxClearanceDetailList()) {
                    if("1".equals(taxClearanceDetail.getType())){
                        // 计算资方利息
                        Double investorDetailRatio = NumberUtils.div(Double.parseDouble(taxClearanceDetail.getInterestRate()), 100); //资方利率
                        Double investorDetailDays = 0D;
                        if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) <= 0){
                            investorDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), now);
                        } else if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) > 0){
                            investorDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), taxClearanceDetail.getEndDate());
                        }
                        Double interest1 = NumberUtils.mul(NumberUtils.div(NumberUtils.mul(prepaidAmount, investorDetailRatio),investorYearDay), investorDetailDays);
                        investorInterestTotal1 = NumberUtils.add(investorInterestTotal1, interest1);
                    }
                }
                carPreSettlementInvestor4.setPreSettlementAmount(NumberUtils.sub(investorInterestTotal1, interest));// 减去第一行的利息
                carPreSettlementInvestor4.setPreSettlementBalance(idYanqilixiBalance);
                carPreSettlementInvestor4.setDifference(NumberUtils.sub(NumberUtils.sub(investorInterestTotal1, interest),idYanqilixiBalance));
                carPreSettlementInvestor4.setType(AccountType.DSYQLX.getInfo());
                carPreSettlementInvestor4.setTypeId(AccountType.DSYQLX.getCode());
                carPreSettlementInvestor4.setCurrencyId(taxClearance.getCurrency());
                carPreSettlementInvestorList.add(carPreSettlementInvestor4);

                result.setCarPreSettlementInvestorList(carPreSettlementInvestorList);

                Double cddyhLixi = 0D; // 利息
                Double customerDays = 0D; // 客户天数差



                // 客户通关缴税-本金
                CarPreSettlementCustomer carPreSettlementCustomer3 = new CarPreSettlementCustomer();
                carPreSettlementCustomer3.setCarId(clearance.getCarId());
                carPreSettlementCustomer3.setCarName(clearance.getTcar().getCarName());
                carPreSettlementCustomer3.setFrameNumber(clearance.getFrameNumber());
                carPreSettlementCustomer3.setAmount(clearance.getForeignCurrencyPrice());
                carPreSettlementCustomer3.setCurrency(clearance.getCurrency());
                carPreSettlementCustomer3.setPreSettlementAmount(prepaidAmount);
                carPreSettlementCustomer3.setPreSettlementBalance(cdBenjinBalance);
                carPreSettlementCustomer3.setDifference(NumberUtils.sub(prepaidAmount, cdBenjinBalance));
                carPreSettlementCustomer3.setType(AccountType.DSBJ.getInfo());
                carPreSettlementCustomer3.setTypeId(AccountType.DSBJ.getCode());
                carPreSettlementCustomer3.setCurrencyId(taxClearance.getCurrency());
                carPreSettlementCustomerList.add(carPreSettlementCustomer3);

                // 客户通关缴税-利息
                CarPreSettlementCustomer carPreSettlementCustomer4 = new CarPreSettlementCustomer();
                carPreSettlementCustomer4.setCarId(clearance.getCarId());
                carPreSettlementCustomer4.setCarName(clearance.getTcar().getCarName());
                carPreSettlementCustomer4.setFrameNumber(clearance.getFrameNumber());
                carPreSettlementCustomer4.setAmount(clearance.getForeignCurrencyPrice());
                carPreSettlementCustomer4.setCurrency(clearance.getCurrency());
                Double interest2 = 0D;
                for (TaxClearanceDetail taxClearanceDetail : taxClearance.getTaxClearanceDetailList()) {
                    if("2".equals(taxClearanceDetail.getType())){
                        Double customerDetailRatio = NumberUtils.div(Double.parseDouble(taxClearanceDetail.getInterestRate()), 100); //客户利率
                        Double customerDetailDays = 0D;
                        if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) <= 0){
                            customerDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), now);
                        } else if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) > 0){
                            customerDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), taxClearanceDetail.getEndDate());
                        }
                        interest2 = NumberUtils.mul(NumberUtils.div(NumberUtils.mul(prepaidAmount, customerDetailRatio),360), customerDetailDays);
                        break;
                    }
                }
                carPreSettlementCustomer4.setPreSettlementAmount(interest2);
                carPreSettlementCustomer4.setPreSettlementBalance(cdLixiBalance);
                carPreSettlementCustomer4.setDifference(NumberUtils.sub(interest2, cdLixiBalance));
                carPreSettlementCustomer4.setType(AccountType.DSLX.getInfo());
                carPreSettlementCustomer4.setTypeId(AccountType.DSLX.getCode());
                carPreSettlementCustomer4.setCurrencyId(taxClearance.getCurrency());
                carPreSettlementCustomerList.add(carPreSettlementCustomer4);

                // 客户通关缴税-延期利息
                CarPreSettlementCustomer carPreSettlementCustomer5 = new CarPreSettlementCustomer();
                carPreSettlementCustomer5.setCarId(clearance.getCarId());
                carPreSettlementCustomer5.setCarName(clearance.getTcar().getCarName());
                carPreSettlementCustomer5.setFrameNumber(clearance.getFrameNumber());
                carPreSettlementCustomer5.setAmount(clearance.getForeignCurrencyPrice());
                carPreSettlementCustomer5.setCurrency(clearance.getCurrency());
                Double customerInterestTotal1 = 0D;//客户延期利息合计
                for (TaxClearanceDetail taxClearanceDetail : taxClearance.getTaxClearanceDetailList()) {
                    if ("2".equals(taxClearanceDetail.getType())) {
                        Double customerDetailRatio = NumberUtils.div(Double.parseDouble(taxClearanceDetail.getInterestRate()), 100); //客户利率
                        Double customerDetailDays = 0D;
                        if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) <= 0){
                            customerDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), now);
                        } else if(now.compareTo(taxClearanceDetail.getStartDate()) >= 0 && now.compareTo(taxClearanceDetail.getEndDate()) > 0){
                            customerDetailDays = DateUtils.getDistanceOfTwoDate(taxClearanceDetail.getStartDate(), taxClearanceDetail.getEndDate());
                        }
                        Double interest3 = NumberUtils.mul(NumberUtils.div(NumberUtils.mul(prepaidAmount, customerDetailRatio),360), customerDetailDays);
                        customerInterestTotal1 = NumberUtils.add(customerInterestTotal1, interest3);
                    }
                }
                carPreSettlementCustomer5.setPreSettlementAmount(NumberUtils.sub(customerInterestTotal1, interest2));// 减去第一行利息
                carPreSettlementCustomer5.setPreSettlementBalance(cdYanqilixiBalance);
                carPreSettlementCustomer5.setDifference(NumberUtils.sub(NumberUtils.sub(customerInterestTotal1, interest2), cdYanqilixiBalance));
                carPreSettlementCustomer5.setType(AccountType.DSYQLX.getInfo());
                carPreSettlementCustomer5.setTypeId(AccountType.DSYQLX.getCode());
                carPreSettlementCustomer5.setCurrencyId(taxClearance.getCurrency());
                carPreSettlementCustomerList.add(carPreSettlementCustomer5);

                result.setCarPreSettlementCustomerList(carPreSettlementCustomerList);
            }

            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping(value = "ok")
    @ResponseBody
    public String ok(@RequestParam("carPresettlementId") String carPresettlementId) {
        try {
            TcarPresettlement tcarPresettlement = new TcarPresettlement(carPresettlementId);
            tcarPresettlement.setStatus1("3");
            tcarPresettlement.setApproveStatus("3");
            tcarPresettlement=tcarPresettlementService.get(carPresettlementId);
            String ret=createAPAR(tcarPresettlement);
            if(ret.equals("操作成功")){
                tcarPresettlementService.update(tcarPresettlement);
            }
            return renderResult(Global.TRUE, text(ret));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    private String createAPAR(TcarPresettlement tcarPresettlement) {
        try {
            TaxClearance taxClearance = new TaxClearance();
            taxClearance.setFrameNumber(tcarPresettlement.getFrameNumber());
            taxClearance.setStatus1("1");
            TaxClearance taxClearance1 = taxClearanceService.findList(taxClearance).get(0);

            BillNegotiated bill = new BillNegotiated();
            bill.setFrameNumber(tcarPresettlement.getFrameNumber());
            bill.setStatus1("1");
            BillNegotiated billNegotiated1 = billNegotiatedService.findList(bill).get(0);

            // 生成预收
            TadvancesReceived tadvancesReceived = new TadvancesReceived();
            tadvancesReceived.setCustomerId(tcarPresettlement.getCustomerId());
            tadvancesReceived.setDate(new Date());
            tadvancesReceived.setStatus1("1");
            tadvancesReceived.setServiceOrderId(tcarPresettlement.getServiceOrderId());
            tadvancesReceived.setCurrency("1");// 人民币
            tadvancesReceived.setBillStatus("2");// 已提交
            tadvancesReceived.setParentBillType("CarPreSettlement");
            tadvancesReceived.setParentBillCode(tcarPresettlement.getCarPresettlementId());
            Double total = 0D;
            List<TadvancesReceivedEstimated> tadvancesReceivedEstimatedList = ListUtils.newArrayList();
            for (TcarPresettlementDetail carPresettlementDetail : tcarPresettlement.getTcarPresettlementDetailList()) {
                if (carPresettlementDetail.getType()==2) {
                    continue;
                }

                TadvancesReceivedEstimated tadvancesReceivedEstimated = new TadvancesReceivedEstimated();
                tadvancesReceivedEstimated.setFrameNumber(carPresettlementDetail.getFrameNumber());
                tadvancesReceivedEstimated.setAdvancesReceivedType(carPresettlementDetail.getChargeType());
                tadvancesReceivedEstimated.setEstimatedReceivedAmount(carPresettlementDetail.getBalance());
                tadvancesReceivedEstimated.setReceivedAmountActual(0D);
                tadvancesReceivedEstimated.setCarName1(carPresettlementDetail.getCarName());
                tadvancesReceivedEstimated.setRowNumber(tcarPresettlement.getRowNumber());
                //tadvancesReceivedEstimated.setReceivedAmountEstimated(carPresettlementDetail.getBalance());
                total = NumberUtils.add(total, carPresettlementDetail.getBalance());
                tadvancesReceivedEstimatedList.add(tadvancesReceivedEstimated);
            }
            tadvancesReceived.setEstimatedReceivedAmount(total);
            tadvancesReceived.setTadvancesReceivedEstimatedList(tadvancesReceivedEstimatedList);
            tadvancesReceivedService.save(tadvancesReceived);

            // 生成预付
            Tprepayment tprepayment = new Tprepayment();
            tprepayment.setSupplierId(serviceOrderService.get(tcarPresettlement.getServiceOrderId()).getInvestorId());
            tprepayment.setDate(new Date());
            tprepayment.setStatus1("1");
            tprepayment.setServiceOrderId(tcarPresettlement.getServiceOrderId());
            tprepayment.setCurrency("1");// 人民币
            tprepayment.setBillStatus("2");// 已提交
            tprepayment.setParentBillType("CarPreSettlement");
            tprepayment.setParentBillCode(tcarPresettlement.getCarPresettlementId());
            Double total1 = 0D;
            List<TprepaymentEstimated> tprepaymentEstimatedList = ListUtils.newArrayList();
            for (TcarPresettlementDetail carPresettlementDetail : tcarPresettlement.getTcarPresettlementDetailList()) {
                if (carPresettlementDetail.getType()==1) {
                    continue;
                }
                TprepaymentEstimated tprepaymentEstimated = new TprepaymentEstimated();
                tprepaymentEstimated.setFrameNumber(carPresettlementDetail.getFrameNumber());
                tprepaymentEstimated.setPrepaymentType(carPresettlementDetail.getChargeType());
                tprepaymentEstimated.setEstimatedPaymentAmount(carPresettlementDetail.getBalance());
                tprepaymentEstimated.setPaymentAmountActual(0D);
                tprepaymentEstimated.setCarName(carPresettlementDetail.getCarName());
                tprepaymentEstimated.setRowNumber(tcarPresettlement.getRowNumber());
                //tprepaymentEstimated.setPaymentAmountEstimated(carPresettlementDetail.getBalance());
                total = NumberUtils.add(total1, carPresettlementDetail.getBalance());
                tprepaymentEstimatedList.add(tprepaymentEstimated);
            }
            tprepayment.setEstimatedPaymentAmount(total1);
            tprepayment.setTprepaymentEstimatedList(tprepaymentEstimatedList);
            tprepaymentService.save(tprepayment);

            // 回写通关缴税
            TaxClearance tax = new TaxClearance();
            tax.setTaxClearanceId(taxClearance1.getTaxClearanceId());
            tax.setPrepaymentId(tprepayment.getPrepaymentId());
            tax.setAdvancesReceivedId(tadvancesReceived.getAdvancesReceivedId());
            taxClearanceService.update(tax);

            // 回写到单押汇
            BillNegotiated billNegotiated = new BillNegotiated();
            billNegotiated.setBillNegotiatedId(billNegotiated1.getBillNegotiatedId());
            billNegotiated.setPrepaymentId(tprepayment.getPrepaymentId());
            billNegotiated.setAdvancesReceivedId(tadvancesReceived.getAdvancesReceivedId());
            billNegotiated.setInvestorEstimatedExchangeRate(tcarPresettlement.getPresttlementRate().toString());
            billNegotiated.setCustomerEstimatedExchangeRate(tcarPresettlement.getPresttlementRate().toString());
            billNegotiatedService.update(billNegotiated);
            return "操作成功";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }


    @RequestMapping(value = "reject")
    @ResponseBody
    public String reject(@RequestParam("carPresettlementId") String carPresettlementId) {
        try {
            TcarPresettlement tcarPresettlement = new TcarPresettlement(carPresettlementId);
            tcarPresettlement.setStatus1("1");
            tcarPresettlement.setApproveStatus("4");
            tcarPresettlementService.update(tcarPresettlement);
            return renderResult(Global.TRUE, text("操作成功！"));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @RequestMapping(value = "cancel")
    @ResponseBody
    public String cancel(@RequestParam("carPresettlementId") String carPresettlementId) {
        try {
            TcarPresettlement tcarPresettlement = new TcarPresettlement(carPresettlementId);
            tcarPresettlement.setStatus1("6");
            tcarPresettlementService.update(tcarPresettlement);
            return renderResult(Global.TRUE, text("作废成功！"));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取服务订单列表
     *
     * @param serviceOrder
     */
    @RequestMapping(value = "getServiceOrderList")
    @ResponseBody
    public List<ServiceOrder> getServiceOrderList(ServiceOrder serviceOrder) {
        serviceOrder.setCustomer(customerService.get(serviceOrder.getCustomerId()));
        /*serviceOrder.setOrderStatus("3");
        serviceOrder.setApproveStatus("3");*/
        List<ServiceOrder> list = serviceOrderService.findList(serviceOrder);
        return list;
    }

    /**
     * 获取车架号列表
     *
     * @param serviceOrderId
     */
    @RequestMapping(value = "getFrameNumberList")
    @ResponseBody
    public List<String> getFrameNumberList(@RequestParam("serviceOrderId") String serviceOrderId) {
        List<String> list = ListUtils.newArrayList();
        TcarPresettlement tcarPresettlement=new TcarPresettlement();
        tcarPresettlement.setServiceOrderId(serviceOrderId);
        list =tcarPresettlementDao.getFrameNumberListByServiceOrderId(tcarPresettlement);
        /*List<ServiceOrderDetail> serviceOrderDetailList = serviceOrderService.get(serviceOrderId).getServiceOrderDetailList();
        for (ServiceOrderDetail serviceOrderDetail : serviceOrderDetailList) {
            list.add(serviceOrderDetail.getFrameNumber());
        }*/
        return list;
    }
    /**
     * 获取车辆名称和行号列表
     *
     * @param serviceOrderId
     */
    @RequestMapping(value = "getCarNameAndRowNumberList")
    @ResponseBody
    public List<Object> getCarNameAndRowNumberList(@RequestParam("serviceOrderId") String serviceOrderId,@RequestParam("frameNumber") String frameNumber) {
        List<Object> list = ListUtils.newArrayList();
        List<ServiceOrderDetail> serviceOrderDetailList = serviceOrderService.get(serviceOrderId).getServiceOrderDetailList();
        for (ServiceOrderDetail serviceOrderDetail : serviceOrderDetailList) {
            if (serviceOrderDetail.getFrameNumber().equals(frameNumber)){
                Object obj=new TcarType().getValue(serviceOrderDetail.getCarId());
                list.add(((Tcar)obj).getCarName());
                list.add(serviceOrderDetail.getRowNumber());
            }
        }
        return list;
    }
/**
 * 提交按钮
 */
    @RequiresPermissions("settlement:carpresettlement:carPreSettlement:submit")
    @PostMapping(value = "submit")
    @ResponseBody
    @Transactional(readOnly=false)
    public String submit(@RequestBody TcarPresettlement carPreSettlement) {
        tcarPresettlementService.save(carPreSettlement);
        return renderResult(Global.TRUE, text("提交成功！"));
    }
  /*
    @RequiresPermissions("settlement:carpresettlement:carPreSettlement:submit")
    @PostMapping(value = "submit")
    @ResponseBody
    @Transactional(readOnly=false)
    public String submit(@RequestBody CarPreSettlement carPreSettlement) {
        TaxClearance taxClearance = new TaxClearance();
        taxClearance.setFrameNumber(carPreSettlement.getFrameNumber());
        taxClearance.setStatus1("1");
        TaxClearance taxClearance1 = taxClearanceService.findList(taxClearance).get(0);

        BillNegotiated bill = new BillNegotiated();
        bill.setFrameNumber(carPreSettlement.getFrameNumber());
        bill.setStatus1("1");
        BillNegotiated billNegotiated1 = billNegotiatedService.findList(bill).get(0);

        // 生成预收
        TadvancesReceived tadvancesReceived = new TadvancesReceived();
        tadvancesReceived.setCustomerId(carPreSettlement.getCustomerId());
        tadvancesReceived.setDate(new Date());
        tadvancesReceived.setStatus1("1");
        tadvancesReceived.setServiceOrderId(carPreSettlement.getServiceOrderId());
        tadvancesReceived.setCurrency("1");// 人民币
        tadvancesReceived.setBillStatus("2");// 已提交
        tadvancesReceived.setParentBillType("CarPreSettlement");
        Double total = 0D;
        List<TadvancesReceivedEstimated> tadvancesReceivedEstimatedList = ListUtils.newArrayList();
        for (CarPreSettlementCustomer carPreSettlementCustomer : carPreSettlement.getCarPreSettlementCustomerList()) {
            TadvancesReceivedEstimated tadvancesReceivedEstimated = new TadvancesReceivedEstimated();
            tadvancesReceivedEstimated.setFrameNumber(carPreSettlementCustomer.getFrameNumber());
            tadvancesReceivedEstimated.setAdvancesReceivedType(carPreSettlementCustomer.getType());
            tadvancesReceivedEstimated.setEstimatedReceivedAmount(carPreSettlementCustomer.getDifference());
            tadvancesReceivedEstimated.setReceivedAmountActual(0D);
            tadvancesReceivedEstimated.setReceivedAmountEstimated(carPreSettlementCustomer.getDifference());
            total = NumberUtils.add(total,carPreSettlementCustomer.getDifference());
            tadvancesReceivedEstimatedList.add(tadvancesReceivedEstimated);
        }
        tadvancesReceived.setEstimatedReceivedAmount(total);
        tadvancesReceived.setTadvancesReceivedEstimatedList(tadvancesReceivedEstimatedList);
        tadvancesReceivedService.save(tadvancesReceived);

        // 生成预付
        Tprepayment tprepayment = new Tprepayment();
        tprepayment.setSupplierId(serviceOrderService.get(carPreSettlement.getServiceOrderId()).getInvestorId());
        tprepayment.setDate(new Date());
        tprepayment.setStatus1("1");
        tprepayment.setServiceOrderId(carPreSettlement.getServiceOrderId());
        tprepayment.setCurrency("1");// 人民币
        tprepayment.setBillStatus("2");// 已提交
        tprepayment.setParentBillType("CarPreSettlement");
        Double total1 = 0D;
        List<TprepaymentEstimated> tprepaymentEstimatedList = ListUtils.newArrayList();
        for (CarPreSettlementInvestor carPreSettlementInvestor : carPreSettlement.getCarPreSettlementInvestorList()) {
            TprepaymentEstimated tprepaymentEstimated = new TprepaymentEstimated();
            tprepaymentEstimated.setFrameNumber(carPreSettlementInvestor.getFrameNumber());
            tprepaymentEstimated.setPrepaymentType(carPreSettlementInvestor.getType());
            tprepaymentEstimated.setEstimatedPaymentAmount(carPreSettlementInvestor.getDifference());
            tprepaymentEstimated.setPaymentAmountActual(0D);
            tprepaymentEstimated.setPaymentAmountEstimated(carPreSettlementInvestor.getDifference());
            total = NumberUtils.add(total1,carPreSettlementInvestor.getDifference());
            tprepaymentEstimatedList.add(tprepaymentEstimated);
        }
        tprepayment.setEstimatedPaymentAmount(total1);
        tprepayment.setTprepaymentEstimatedList(tprepaymentEstimatedList);
        tprepaymentService.save(tprepayment);

        // 回写通关缴税
        TaxClearance tax = new TaxClearance();
        tax.setTaxClearanceId(taxClearance1.getTaxClearanceId());
        tax.setPrepaymentId(tprepayment.getPrepaymentId());
        tax.setAdvancesReceivedId(tadvancesReceived.getAdvancesReceivedId());
        taxClearanceService.update(tax);

        // 回写到单押汇
        BillNegotiated billNegotiated = new BillNegotiated();
        billNegotiated.setBillNegotiatedId(billNegotiated1.getBillNegotiatedId());
        billNegotiated.setPrepaymentId(tprepayment.getPrepaymentId());
        billNegotiated.setAdvancesReceivedId(tadvancesReceived.getAdvancesReceivedId());
        billNegotiated.setInvestorEstimatedExchangeRate(carPreSettlement.getExchangeRate().toString());
        billNegotiated.setCustomerEstimatedExchangeRate(carPreSettlement.getExchangeRate().toString());
        billNegotiatedService.update(billNegotiated);
        return renderResult(Global.TRUE, text("提交成功！"));
    }*/
}
